package org.keyintegrity.webbeans.metadata.store;

import org.keyintegrity.webbeans.metadata.Appearance;
import org.keyintegrity.webbeans.metadata.BeanMetadata;
import org.keyintegrity.webbeans.metadata.FieldMetadata;
import org.keyintegrity.webbeans.metadata.MetadataManager;
import org.keyintegrity.webbeans.metadata.store.xmlstream.XmlStreamStoreContext;
import org.keyintegrity.webbeans.metadata.store.xmlstream.XmlStreamStoreContext.Scope;

public class WebbeansMetadataStoreBuilder {

    /** Build Webbeans MetadataStore while build is testing */
    public static void buildWebbeansCoreStore() throws Exception {
        XmlStreamStoreContext context = new XmlStreamStoreContext(MetadataManager.WEBBEANS_CORE_METADATA_XMLSTREAM, Scope.BUILD);
        
        MetadataStore store = MetadataStoreFactory.getMetadataStore(context);
        
        store.clear();

        BeanMetadata bm;
        bm = store.acquireBeanMetadataForClass(BeanMetadata.class);
        registerFieldMetadataDefaults(bm, "AssociatedClass", true, Appearance.BOTH, null, null);
        store.storeMetadata(bm);
        
        bm = store.acquireBeanMetadataForClass(FieldMetadata.class);
        registerFieldMetadataDefaults(bm, "FieldName",      true, Appearance.BOTH, null, null);
        registerFieldMetadataDefaults(bm, "ReadOnly",       true, Appearance.BOTH, null, null);
        registerFieldMetadataDefaults(bm, "Appearance",     true, Appearance.BOTH, null, null);
        registerFieldMetadataDefaults(bm, "DisplayPattern", true, Appearance.BOTH, null, null);
        registerFieldMetadataDefaults(bm, "TypeParameter",  true, Appearance.BOTH, null, null);
        store.storeMetadata(bm);
        
        System.out.println("Done Building Webbeans Core Store");
    }

    private static void registerFieldMetadataDefaults(BeanMetadata bm,
            String fieldName, boolean readOnly, Appearance appearance,
            String displayPattern, String typeParameter) {
        FieldMetadata fm = bm.getMetadataForField(fieldName);
        fm.setReadOnly(readOnly);
        fm.setAppearance(appearance);
        fm.setDisplayPattern(displayPattern);
        fm.setTypeParameter(typeParameter);
    }

    /**
     * Driver
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        buildWebbeansCoreStore();
    }
}
